package com.sirdc.modules.paas.api.report;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.sirdc.modules.paas.api.common.CommonResponse;
import com.sirdc.modules.paas.api.common.Constants;
import com.sirdc.modules.paas.api.common.util.CommonUtil;
import com.sirdc.modules.paas.api.common.util.XmlParseUtil;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * 接收短信回执请求返回类.
 * 
 * @author Qing
 * @e-mail lhq@suncco.com
 * @version v1.0
 * @copyright 2010-2015
 * @create-time 2013-9-18 下午5:39:33
 * 
 */
public class ReportResponse extends CommonResponse {

	private static final long serialVersionUID = -8967667797342084998L;
	
	
	private static final String RPTAMOUNT = "rptAmount";
	
	/**
	 * 成功接收短信回执的条数.
	 */
	private String amount;
	/**
	 * 接收短信回执的结果集.
	 */
	private List<ReportInfo> reportList;
	
	/**
	 * 解析请求返回的xml数据,设置返回对象的属性值.
	 * @param xmlStr 请求返回的xml字符串.
	 */
	public ReportResponse(String xmlStr) {
		Map<String, String> map = super.getMapByXmlStr(xmlStr);
		if (map.containsKey(Constants.STATECODE)) {
			this.stateCode = map.get(Constants.STATECODE);
		}
		if (map.containsKey(Constants.STATEINTRO)) {
			this.stateIntro = map.get(Constants.STATEINTRO);
		}
		if (map.containsKey(RPTAMOUNT)) {
			this.amount = map.get(RPTAMOUNT);
			this.xmlParseToReportList(xmlStr);
		}
	}

	/**
	 * 解析请求返回的xml数据,设置接收短信对象列表的属性值.
	 * @param xmlStr 请求返回的xml字符串.
	 */
	private void xmlParseToReportList(String xmlStr) {
	    reportList=new ArrayList<ReportInfo>();
		String tagName = "rpt";
		NodeList nodeList = XmlParseUtil.getNodeListByTagName(XmlParseUtil.parseDoc(xmlStr, Constants.CHARSET), tagName);
		for (int i = 0; i < nodeList.getLength(); ++i) {
			Node node = nodeList.item(i);
			NodeList childNodeList = node.getChildNodes();
			ReportInfo report = new ReportInfo();
			for (int j = 0; j < childNodeList.getLength(); ++j) {
				Node childNode = childNodeList.item(j);
				if (childNode.getNodeName().equals("sn")) {
					report.setSn(CommonUtil.trimString(childNode.getTextContent()));
				} else if (childNode.getNodeName().equals("smsId")) {
					report.setSmsId(CommonUtil.trimString(childNode.getTextContent()));
				} else if (childNode.getNodeName().equals("phoneNumber")) {
					report.setPhoneNumber(CommonUtil.trimString(childNode.getTextContent()));
				} else if (childNode.getNodeName().equals("createTime")) {
					report.setCreateTime(CommonUtil.trimString(childNode.getTextContent()));
				}
			}
			reportList.add(report);
		}
	}
	
	public String getAmount() {
		return amount;
	}
	public void setAmount(String amount) {
		this.amount = amount;
	}
	public List<ReportInfo> getReportList() {
		return reportList;
	}
	public void setReportList(List<ReportInfo> reportList) {
		this.reportList = reportList;
	}	

}
